package com.itany.hotel.service.proxy;

import com.itany.hotel.entity.Subscription;
import com.itany.hotel.entity.SubscriptionDtl;
import com.itany.hotel.service.SubscriptionService;
import framework.bean.factory.ObjectFactory;
import framework.dao.exception.DataAccessException;
import framework.service.exception.ServiceException;
import framework.service.transaction.TransactionManager;

import java.util.List;

public class SubscriptionServiceProxy implements SubscriptionService {

    private SubscriptionService subscriptionService;

    public SubscriptionServiceProxy() {
        subscriptionService = (SubscriptionService) ObjectFactory.getObject("subscriptionServiceTarget");
    }

    @Override
    public List<Subscription> findUnacknowledged(Integer memberId) {


        TransactionManager transactionManager= (TransactionManager) ObjectFactory.getObject("transaction");

        List<Subscription> subscriptions=null;

        try {
            transactionManager.begin();

            subscriptions=subscriptionService.findUnacknowledged(memberId);

            transactionManager.commit();
        } catch (DataAccessException e){
            transactionManager.rollback();
            throw new ServiceException(e);
        }
        return subscriptions;

    }

    @Override
    public List<Subscription> findHistorical(Integer memberId) {
        TransactionManager transactionManager= (TransactionManager) ObjectFactory.getObject("transaction");

        List<Subscription> subscriptions=null;

        try {
            transactionManager.begin();

            subscriptions=subscriptionService.findHistorical(memberId);

            transactionManager.commit();
        } catch (DataAccessException e){
            transactionManager.rollback();
            throw new ServiceException(e);
        }
        return subscriptions;
    }

    @Override
    public void addDetail(List<SubscriptionDtl> subscriptionDtls) {

    }
}
